﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;  //  for Vector2
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D

namespace zombieArena
{
    class Laser : Weapon
    {
        Vector2 midPoint = new Vector2(8f, 8f);

        public Laser(Texture2D t)
            : base(t)
        {
            ammo = 8;
            rateOfFire = 500f;
            bulletSpeed = 50f;
            name = "Laser";
            soundIndex = 3;
        }

        Bullet b;
        public override void shoot(Vector2 position, Vector2 direction, Map map)
        {
            b = new Bullet(position, direction * bulletSpeed, texture, 1, 100f, 0f, 10000f, 0f, midPoint);
            b.rotate = -angleVector(ref direction, ref rotateVector);
            b.midPoint = new Vector2(75, 8);
            map.bulletList.Add(b);
            ammo--;
        }

        Vector2 rotateVector = new Vector2(-1, 0);

        public float angleVector(ref Vector2 v1, ref Vector2 v2)
        {
            return (float)(Math.Atan2(v1.X, v1.Y) - Math.Atan2(v2.X, v2.Y));
        }

        /*
        public void prototype4idle(Vector2 position, Vector2 direction, Map map, SpriteBatch spriteBatch)
        {
            Vector2 laserPointer = position;

            int laserCount = 0;
            while (laserCount < 1000)
            {
                if (laserPointer.X < 0 || laserPointer.X > 1600 || laserPointer.Y < 0 || laserPointer.Y > 1200)
                {
                    return;
                }

                int i = (int)laserPointer.X / map.cellSize;
                int j = (int)laserPointer.Y / map.cellSize;
                int zombieCount = 0;
                while (zombieCount < map.enemyList[i, j].Count)
                {
                    Enemy enemy = map.enemyList[i, j][zombieCount];

                    // distance between bullets and zombies
                    double distance = Math.Sqrt(Math.Pow(laserPointer.X - enemy.position.X, 2.0) + Math.Pow(laserPointer.Y - enemy.position.Y, 2.0));
                    if (distance < 30)
                    {
                        Vector2 enemyscreenpos = enemy.position - new Vector2(map.viewPort.X, map.viewPort.Y);
                        //spriteBatch.Draw(enemy.charTexture, enemyscreenpos, new Rectangle(40 * enemy.currentSprite, 56 * enemy.spriteRow, enemy.width, enemy.height), Color.Red);
                        enemy.draw(enemyscreenpos, spriteBatch, Color.Red);
                        return;
                    }
                    zombieCount++;
                }
                Vector2 laserscreenpos = laserPointer - new Vector2(map.viewPort.X, map.viewPort.Y);

                laserCount++;
                spriteBatch.Draw(texture, laserscreenpos, null, Color.White, 0f, new Vector2(0, 0), 0.2f, SpriteEffects.None, 0f);
                laserPointer = laserPointer + direction;
            }


        }*/
    }
}
